const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const WxParse = require('../Common/wxParse/wxParse.js');
Page({
  data: {
    goodsId:'',
    goodsDetailsdata:'',
    getDefaultAddressdata:'',
    content:'',
    kfconcatnums:'',
    iscart:false,
    number:1,
    cartnums:0,
  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        goodsId:options.id
      })
      this.goodsDetail()
      this.kfconcat()
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow(){
    this.cartcount()
  },
  cartcount(){
    Controller.cartcount().then(res=>{
      this.setData({
        cartnums:res.data.baseData>99?99:res.data.baseData
      })
    })
  },
  adcart(){
    this.setData({
      iscart:true
    })
  },
  closecart(){
    this.setData({
      iscart:false
    })
  },
  mins(){
    var number=this.data.number
    number--;
   
    if(this.data.goodsDetailsdata.stock===0){
        return this.setData({
          number:0
        })
    }

    if(number<=0){
      number=1
    }
    this.setData({
      number
    })
  },
  plus(){
    var number=this.data.number
    number++;
    if(number>this.data.goodsDetailsdata.stock){
      number=this.data.goodsDetailsdata.stock
      wx.showToast({
        title: '库存不足',
        icon:"none"
      })
    }

    this.setData({
      number
    })
  },
  changenums(e){
    var number=e.detail.value;
    if(number>=this.data.goodsDetailsdata.stock){
      number=this.data.goodsDetailsdata.stock
    }
    if(number<=0){
      number=0
    }
    this.setData({
      number
    })
  },
  buy(){

    if(this.data.goodsDetailsdata.stock==0){
        return wx.showToast({
          title: '库存为空',
          icon:'none'
        })
    }

    wx.navigateTo({
      url:"/pages/mall/order?id="+this.data.goodsId+"&type=goods"
    });
  },
  gocart(){
    if(this.data.goodsDetailsdata.stock===0){
     return wx.showToast({
        title: '库存为空',
        icon:"none"
      })
    }
    var data={
      goodsId:this.data.goodsId,
      specs:this.data.goodsDetailsdata.specs,
      amount:this.data.number,
      type:1
    }
    Controller.addOrUpdate(data).then(res=>{
      wx.showToast({
        title:"加购成功"
      })
      this.cartcount()
      setTimeout(()=>{
        wx.navigateTo({
          url:"/pages/mall/cart"
        });
      },1500)
    })
   
  },
  goodsDetail(){
    Controller.goodsdetail({goodsId:this.data.goodsId}).then(res=>{
      if(res.data.baseData){
        if(res.data.baseData.imgUrls){
          res.data.baseData.imgUrls=res.data.baseData.imgUrls.split(',')
        }
       
        this.setData({
          goodsDetailsdata:res.data.baseData
        })
        if(res.data.baseData.stock===0){
          this.setData({
            number:0
          })
        }
       

        if(res.data.baseData.content){
          var content=res.data.baseData.content
          var that = this;
          WxParse.wxParse('content', 'html', content, that, 5);
          res.data.baseData.content = JSON.stringify(res.data.baseData.content).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.content = JSON.parse(res.data.baseData.content);
        }

        this.getDefaultAddress()
      }
    })
  },
  zan(e){
    if(this.data.goodsDetailsdata.hasLike==0){
      this.action(1,true)
    }else{
      var that=this;
      wx.showModal({
        title: '提示',
        content: '是否取消点赞',
        success (res) {
          if (res.confirm) {
            that.action(1,false)
          } else if (res.cancel) {
          }
        }
      })
    }
  },
  action(type,isbool){
    var data={}
    if(isbool){
      data.rId=this.data.goodsId
      data.type=type  //类型，1是点赞，2是关注，3.浏览,5分享，6发布,7.签到8.观看视频
      data.source=5  // 主体对象的类型，1是名医内容，2是专家内容，3.养生视频，4养生资讯，5是商城，6食谱，7名医，8专家
      data.contentType=5  //内容类型，1是文章，2是视频，3是食谱，4.课程，5是商品
      Controller.action(data).then(res=>{
        if(type==1 && isbool){
          wx.showToast({
            title:"点赞成功"
          })
          this.goodsDetail()
        }
      })
    }else{
      data.actionId=this.data.goodsDetailsdata.likeActionId
      Controller.delAction(data).then(res=>{
          wx.showToast({
            title:"取消成功"
          })
          this.goodsDetail()
      })
    }
  },
  getDefaultAddress(){
    Controller.getDefaultAddress().then(res=>{
      if(res.data.baseData){
        this.setData({
          getDefaultAddressdata:res.data.baseData
        })
      }
    })
  },
  shop(e){
    wx.navigateTo({
      url:"/pages/mall/cart"
    });
  },
  kfconcat(){
    Controller.appinfo({key:"key-phone"}).then(res=>{
      if(res.data.baseData){
        this.setData({
          kfconcatnums:res.data.baseData.content
        })
      }
    })
  },
  concat(e){
    wx.showActionSheet({
      itemList: [e.currentTarget.dataset.tel,'呼叫'],
      success:function(res){
        if(res.tapIndex==1){
          wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.tel,
          })
        }
      }
    })
  },
  confirmcart(e){
    let pin=e.currentTarget.dataset.pin;
    if(pin==1){
      this.setData({
        isPin:true
      })
    }else{
      this.setData({
        isPin:false
      })
    }
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    let goods=this.data.confirmcartdata;
    goods.num=this.data.goodsnumber
    goods.isPin=this.data.isPin
    if(this.data.confirmtype=='group'){
      this.goodsSettlement(goods,this.data.marketid,'')
    }else if(this.data.confirmtype=="groupno"){
      this.goodsSettlement(goods,'',this.data.marketNo)
    }else{
      this.addGoods(goods)
    }
  },
  onShareAppMessage: function (e) {
    let title = this.data.goodsDetailsdata.goodsTitle || '';;
    let imageUrl = this.data.goodsDetailsdata.imgUrl || '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    this.action(5,true)
    return obj
  },
  onShareTimeline: function(res){
    let title = this.data.goodsDetailsdata.goodsTitle || '';;
    let imageUrl = this.data.goodsDetailsdata.imgUrl || '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    this.action(5,true)
    return obj
  },

 
})